<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/../includes/lib_order.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_finance.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_supplier.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_goods_attr.php');
require(dirname(__FILE__) . '/includes/lib/cls/cls_date.php');


if($_REQUEST['act'] == 'bank_account_setting')
{
	if((admin_priv('erp_finance_manage', '', false)))
	{	
		$bank_account_info=get_bank_account_info();	
		
		$href="erp_finance_manage.php?act=add_bank_account";
		$text=$_LANG['erp_bank_account_add'];
		
		$action_link = array('href' => $href, 'text' => $text);
		$smarty->assign('action_link',  $action_link);					
			
		$smarty->assign('bank_account_info',  $bank_account_info);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_account_setting']);
		$smarty->display('erp_bank_account.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		
}

if($_REQUEST['act'] == 'add_bank_account')
{
	if((admin_priv('erp_finance_manage', '', false)))
	{	
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_bank_account_add']);
		$smarty->display('erp_bank_account_info.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		
}

elseif($_REQUEST['act'] == 'payment_style_setting')
{
	$payment_style_info=get_payment_style_info();
	$smarty->assign('payment_style_info',  $payment_style_info);
	$smarty->display('erp_payment_style.htm');			
}

elseif($_REQUEST['act'] == 'bank_account_list')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{	
		$bank_account_info=get_bank_account_info();
	
		$smarty->assign('bank_account_info',  $bank_account_info);
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_account_list']);
		$smarty->display('erp_bank_account_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'print_bank_details')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{		
		$account_id=$_REQUEST['account_id'];
		$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
		$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';		
		
		$bank_account_info=get_bank_account_info($account_id);
		$bank_details=get_bank_account_details($account_id,$start_date,$end_date);
		
		$smarty->assign('bank_account_info',  $bank_account_info);	
		$smarty->assign('bank_details',  $bank_details);
		$smarty->display('erp_bank_account_details_print.htm');			
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}

elseif($_REQUEST['act'] == 'view_bank_details')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{		
		include('./includes/lib/page.class.php');
		
		//分页类参数
		$num_per_page=10;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
		$start=$num_per_page*($page-1);				
				
		$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
		$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';		
		
		$cls_date=new cls_date();
		
		if(!empty($start_date))
		{
			$start_time=$cls_date->date_to_stamp($start_date);
		}
		if(!empty($end_date))
		{
			$end_time=$cls_date->date_to_stamp($end_date);
		}		
		
		
		$account_id=$_REQUEST['account_id'];
	
		$total_num=count(get_bank_account_details($account_id,$start_time,$end_time));
	
		$bank_details=get_bank_account_details($account_id,$start_time,$end_time,$start,$num_per_page);

		$url="erp_finance_manage.php?act=view_bank_details&account_id=".$account_id."&s_date=".$start_date."&e_date=".$end_date;

		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());

		$bank_account_info=get_bank_account_info($account_id);
		
		$smarty->assign('start_date',  $start_date);
		$smarty->assign('end_date',  $end_date);		
		$smarty->assign('bank_account_info',  $bank_account_info);	
		$smarty->assign('bank_details',  $bank_details);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_bank_account_details']);
		$smarty->display('erp_bank_account_details.htm');	
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}
}

elseif($_REQUEST['act'] == 'account_payable')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{	
		include('./includes/lib/page.class.php');
		
		//分页类参数
		$num_per_page=10;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类	
		
		$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
		$start=$num_per_page*($page-1);	
		
		$filter=isset($_REQUEST['filter'])?($_REQUEST['filter']):'payable';
		$order_sn=isset($_REQUEST['order_sn'])?($_REQUEST['order_sn']):'';
		/* 时间参数 */
		$start_date = empty($_REQUEST['start_date']) ? local_strtotime('-2 months') : local_strtotime($_REQUEST['start_date']);
		$end_date = empty($_REQUEST['end_date']) ? local_strtotime('today') : local_strtotime($_REQUEST['end_date']);
	
		if(!empty($order_sn))
		{
			$sql="select order_id from ".$GLOBALS['ecs']->table('erp_order')." where order_sn='".$order_sn."'";
			$order_id=$GLOBALS['db']->getOne($sql);		
		}
		if(!empty($order_sn) && empty($order_id))
		{
				$total_num=0;
				$payables_info=array();
			}
		else{
			$total_num=count(get_payable_order($order_id,$filter));
			$payables_info=get_payable_order($order_id,$filter,$start,$num_per_page, $start_date, $end_date);			
		}

		$url="erp_finance_manage.php?act=payables_list&order_sn=".$order_sn."&filter=".$filter;

		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());
		
		if((admin_priv('erp_finance_manage', '', false)))
		{
			$smarty->assign('act',  'edit');
		}
	
		//echo "<pre>";
			//print_r($payables_info);
		//echo "</pre>";
		
		$smarty->assign('start_date',     local_date('Y-m-d', $start_date));
		$smarty->assign('end_date',       local_date('Y-m-d', $end_date));
		$smarty->assign('payable_info',  $payables_info);
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_account_payable_list']);
		$smarty->display('erp_account_payable_list.htm');							
				
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}
elseif($_REQUEST['act'] == 'view_payable')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{	
		$order_id=isset($_REQUEST['order_id'])?($_REQUEST['order_id']):'';
		$payables_info=get_payable_order($order_id,'payable');	

		if((admin_priv('erp_finance_manage', '', false)))
		{
			$smarty->assign('act',  'edit');
		}	
		
		$smarty->assign('payable_info',  $payables_info[$order_id]);
		
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_payable_details']);
		$smarty->display('erp_payable.htm');			
				
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'view_receivable')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{	
		$order_id=isset($_REQUEST['order_id'])?($_REQUEST['order_id']):'';
		$receivable_info=get_receivable_order($order_id);	
	
	//echo "<pre>";
		//print_r($receivable_info);
	//echo "</pre>";

		if((admin_priv('erp_finance_manage', '', false)))
		{
			$smarty->assign('act',  'edit');
		}	
		
		$smarty->assign('receivable_info',  $receivable_info[0]);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_receivable_details']);
		$smarty->display('erp_receivable.htm');			
				
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'payment_list')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{
		$payment_status=get_payment_status();
		$smarty->assign('payment_status',  $payment_status);
		
		$supplier_info=get_supplier_info();
		$smarty->assign('supplier_info',  $supplier_info);
				
		include('./includes/lib/page.class.php');
		
		//分页类参数
		$num_per_page=10;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		if(!empty($_REQUEST['payment_sn']))
		{
			$payment_info=get_payment_info('',$_REQUEST['payment_sn']);
			$total_num=count($payment_info);
		}
		else{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
			$start=$num_per_page*($page-1);
			
			$payment_status=isset($_REQUEST['p_s'])?($_REQUEST['p_s']):0;
			
			/*
			if((admin_priv('erp_finance_manage', '', false)) && !(admin_priv('erp_finance_approve', '', false)))
			{
				$payment_status=1;
			}	
			elseif(!(admin_priv('erp_finance_manage', '', false)) && (admin_priv('erp_finance_approve', '', false)))
			{
				$payment_status=2;
			}					
			else{
				$payment_status=0;
			}
			*/
		
			$pay_to=isset($_REQUEST['pay_to'])?($_REQUEST['pay_to']):0;
			$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
			$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';
			
			$cls_date=new cls_date();
			
			if(!empty($start_date))
			{
				$start_time=$cls_date->date_to_stamp($start_date);
			}
			if(!empty($end_date))
			{
				$end_time=$cls_date->date_to_stamp($end_date);
			}	

			$total_num=count(get_payment_info('','',$payment_status,$start_time,$end_time,$pay_to));
			
			$payment_info=get_payment_info('','',$payment_status,$start_time,$end_time,$pay_to,$start,$num_per_page);

			$url="erp_finance_manage.php?act=payment_list&p_s=".$payment_status."&pay_to=".$pay_to."&s_date=".$start_date."&e_date=".$end_date;
		}

		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());		

    $smarty->assign('bank_account',  get_bank_account_info('','1'));
    $smarty->assign('payable_orders',  get_payable_order());		
		$smarty->assign('payment_info',  $payment_info);
		
		//echo "<pre>";
			//print_r(get_bank_account_info('','1'));
		//echo "</pre>";		
				
		//echo "<pre>";
			//print_r($payment_info);
		//echo "</pre>";
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_payment_list']);
		$smarty->display('erp_payment_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'edit_payment')
{
	if((admin_priv('erp_finance_manage', '', false)))
	{	
		$payment_id=$_REQUEST['payment_id'];
		if(lock_payment($payment_id,'edit'))//锁定付款单
		{
			$payment_info=get_payment_info($payment_id);
		
			//echo "<pre>";
				//print_r($payment_info);
			//echo "</pre>";
			//exit();
		
			$smarty->assign('payment_info',  $payment_info[0]);
			
			$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_payment_list']);
			$smarty->display('erp_payment_info.htm');		
		}
		else{
			$href="./erp_finance_manage.php?act=payment_list";
			$text=$_LANG['erp_payment_return_to_payment_list'];
			$link[] = array('href' => $href, 'text' => $text);	
			
			$href="./erp_finance_manage.php?act=view_payment&payment_id=".$payment_id;
			$text=$_LANG['erp_payment_view_payment'];
			$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_order_no_accessibility'], 0, $link);
		}		
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'receivable_list')
{
	//echo "<pre>";
		//print_r(get_receivable_order());
	//echo "</pre>";
	//exit();
	
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{	
		include('./includes/lib/page.class.php');
		
		//分页类参数
		$num_per_page=20;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类	
		
		$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
		$start=$num_per_page*($page-1);	
		
		$order_sn=isset($_REQUEST['order_sn'])?($_REQUEST['order_sn']):'';
		
		if(!empty($order_sn))
		{
			$sql="select order_id from ".$GLOBALS['ecs']->table('order_info')." where order_sn='".$order_sn."'";
			$order_id=$GLOBALS['db']->getOne($sql);	
		}
		
		//echo $order_id;
		//exit();
		
		if(!empty($order_sn) && empty($order_id))
		{
				$total_num=0;
				$receivable_info=array();
			}
		else{
			$total_num=count(get_receivable_order($order_id));
			$receivable_info=get_receivable_order($order_id,$start,$num_per_page);			
		}

		$url="erp_finance_manage.php?act=receivable_list&order_sn=".$order_sn;

		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());
		
		if((admin_priv('erp_finance_manage', '', false)))
		{
			$smarty->assign('act',  'edit');
		}
		
		//echo "<pre>";
			//print_r($receivable_info);
		//echo "</pre>";
	
		$smarty->assign('receivable_info',  $receivable_info);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_account_receivable_list']);
		$smarty->display('erp_receivable_list.htm');		
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'gathering_list')
{
	if(admin_priv('erp_finance_view', '', false) || admin_priv('erp_finance_manage', '', false) || admin_priv('erp_finance_approve', '', false))
	{
		$gathering_status=get_gathering_status();
		$smarty->assign('gathering_status',  $gathering_status);
				
		include('./includes/lib/page.class.php');
		
		//分页类参数
		$num_per_page=10;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		if(!empty($_REQUEST['gathering_sn']))
		{
			$gathering_info=get_gathering_info('',$_REQUEST['gathering_sn']);
			$total_num=count($gathering_info);
		}
		else{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
			$start=$num_per_page*($page-1);
			
			//echo $start;
			//echo $num_per_page;
			
			$gathering_status=isset($_REQUEST['g_s'])?($_REQUEST['g_s']):0;
			
			/*
			if((admin_priv('erp_finance_manage', '', false)) && !(admin_priv('erp_finance_approve', '', false)))
			{
				$gathering_status=1;
			}	
			elseif(!(admin_priv('erp_finance_manage', '', false)) && (admin_priv('erp_finance_approve', '', false)))
			{
				$gathering_status=2;
			}					
			else{
				$gathering_status=0;
			}			
			*/
			
			$pay_from=isset($_REQUEST['pay_from'])?($_REQUEST['pay_from']):0;
			$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
			$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';

			$cls_date=new cls_date();
			
			if(!empty($start_date))
			{
				$start_time=$cls_date->date_to_stamp($start_date);
			}
			if(!empty($end_date))
			{
				$end_time=$cls_date->date_to_stamp($end_date);
			}		

			$total_num=count(get_gathering_info('','',$gathering_status,$start_time,$end_time,$pay_from));
			
			$gathering_info=get_gathering_info('','',$gathering_status,$start_time,$end_time,$pay_from,$start,$num_per_page);
			
			$url="erp_finance_manage.php?act=gathering_list&g_s=".$gathering_status."&pay_from=".$pay_from."&s_date=".$start_date."&e_date=".$end_date;
		}

		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());		
	

    $smarty->assign('bank_account',  get_bank_account_info());
    $smarty->assign('receivable_order',  get_receivable_order());		
		$smarty->assign('gathering_info',  $gathering_info);
		
		//echo "<pre>";
			//print_r(get_receivable_order());
		//echo "<pre>";		
		
		//echo "<pre>";
			//print_r($gathering_info);
		//echo "<pre>";
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_gathering_list']);
		$smarty->display('erp_gathering_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif($_REQUEST['act'] == 'print_gathering')
{
	echo "print_gathering";
}

elseif($_REQUEST['act'] == 'print_payment')
{
	echo "print_payment";
}

?>